<template>
	<view v-if="userDetail" class="container" :style="[{
				transform: coverTransform,
				transition: coverTransition
			}]">
		<!--  @touchstart="coverTouchstart"
	 @touchmove="coverTouchmove" @touchend="coverTouchend" -->
		<view class="user-section" v-if="userDetail">
			<image class="bg" src="https://www.ggshop1688.com/upload/api-img/user-bg.jpg"></image>
			<view class="user-info-box">
				<view class="portrait-box" @click="editUserinfo">
					<image class="portrait" :src="user.pic ? user.pic  : userDetail.userPhoto"></image>
				</view>
				<view class="info-box">
<!--					<view class="username">{{username?username:userDetail.userName}}</view>-->
					<view class="username">{{userDetail.userPhone}}</view>
					<view class="info-tips">消费等级：{{userDetail.rankName}}</view>
          <view class="referrer" v-if="parentUser">推荐人：{{parentUser.userPhone}}</view>
          <view class="info-tips" v-if="userDetail.aagentName">代理等级：{{userDetail.aagentName}}</view>
				</view>
				<!-- <view class="vipInfo" style="text-align: center;" v-if="userDetail">
					<view style="color: #000;">
						<text>{{userDetail.rankName}}</text>
					</view>
				</view> -->

        <view class="sign-box" @click="linkto('/pages/user/sign')">
          <image class="sign-icon" src="/static/icon/sign.png"></image>
          <text class="sign-title">签到</text>
        </view>
			</view>
			<view class="tj-sction">
				<view class="tj-item" @click="linkto('/subPackage/balance/balance')">
					<text class="num">{{userDetail?userDetail.userMoney:0}}</text>
					<text>余额</text>
				</view>
				<view class="tj-item" @click="linkto('/subPackage/freecharge/freecharge')">
					<text class="num">{{userDetail?userDetail.vip_free_left_num:0}}</text>
					<text>免单数量</text>
				</view>
				<view class="tj-item" @click="linkto('/subPackage/chartsTree/chartsTree?userImg='+userDetail.userPhoto)">
					<text class="num">{{userDetail?userDetail.vip_num:0}}</text>
					<text>vip数量</text>
				</view>
				<view class="tj-item" @click="toassetbag">
					<text class="num">{{userDetail?userDetail.gg_credit:0}}</text>
					<text>消费奖励</text>
				</view>
			</view>
			<!-- <view class="vip-card-box">
				<image class="card-bg" src="/static/vip-card-bg.png" mode=""></image>
				<view class="b-btn">
					立即开通
				</view>
				<view class="tit">
					<text class="yticon icon-iLinkapp-"></text>
					获取推广资格
				</view>
				<text class="e-m">DCloud Union</text>
				<text class="e-b">开通会员开发无bug 一测就上线</text>
			</view> -->
		</view>
		<view class="user-section" v-else @click="editUserinfo">
			<image class="bg" src="/static/user-bg.jpg"></image>
			<view class="user-info-box">
				<view class="portrait-box">
					<image class="portrait" src="/static/missing-face.png"></image>
				</view>
				<view class="info-box">
					<text class="username">点击登录账号</text>
				</view>
				<view class="vipInfo">
					<image :src="vipEnable" class="vipIcon"></image>
					<view>普通用户</view>
				</view>
			</view>
			<!-- <view class="vip-card0000000000000-box">
				<image class="card-bg" src="/static/vip-card-bg.png" mode=""></image>
				<view class="b-btn" @click="clickTest">
					立即开通
				</view>
				<view class="tit">
					<text class="yticon icon-iLinkapp-"></text>
					获取推广资格
				</view>
				<text class="e-m">DCloud Union</text>
				<text class="e-b">开通会员开发无bug 一测就上线</text>
			</view> -->
		</view>
		<view class="cover-container">
			<!-- @touchstart="coverTouchstart" @touchmove="coverTouchmove" @touchend="coverTouchend" -->
			<!-- <image class="arc" src="/static/arc.png"></image> -->


			<view class="order-section">
				<view class="order-item" hover-class="common-hover" :hover-stay-time="50" @click="clickTest">
					<image src="../../static/images/recharge.png" class="itmePic"></image>
					<text>线下购车</text>
				</view>
				<view class="order-item" hover-class="common-hover" @click="linkto('/subPackage/chartsTree/chartsTree?userImg='+userDetail.userPhoto)"
				 :hover-stay-time="50">
					<image src="../../static/images/cust.png" class="itmePic"></image>
					<text>我的团队</text>
				</view>
				<view class="order-item" hover-class="common-hover" @click="linkto('/subPackage/sharepage/sharepage')"
				 :hover-stay-time="50">
					<image src="../../static/images/share.png" class="itmePic"></image>
					<text>分享</text>
				</view>
				<view class="order-item" hover-class="common-hover" :hover-stay-time="50" @click="linkto('/subPackage/freecharge/freecharge')">
					<image src="../../static/images/history.png" class="itmePic"></image>
					<text>免单记录</text>
				</view>
			</view>

<!--      <view class="order-section user-order">-->
<!--        <view class=" order">-->
<!--          商家订单管理-->
<!--        </view>-->
<!--        <view>-->
<!--          <button type="button" class="button" @click="linkto(`/pages/order/writeOff`)">自提订单核销</button>-->
<!--        </view>-->
<!--      </view>-->
			<!-- 订单 -->
			<view class="order-section">
				<view class="order-item" @click="navTo('/pages/order/order?state=0')" hover-class="common-hover" :hover-stay-time="50">
					<text class="yticon icon-shouye"></text>
					<text>全部订单</text>
				</view>
				<view class="order-item" @click="navTo('/pages/order/order?state=1')" hover-class="common-hover" :hover-stay-time="50">
					<text class="yticon icon-daifukuan"></text>
					<text>待付款</text>
				</view>
				<view class="order-item" @click="navTo('/pages/order/order?state=2')" hover-class="common-hover" :hover-stay-time="50">
					<text class="yticon icon-yishouhuo"></text>
					<text>待收货</text>
				</view>
				<view class="order-item" @click="navTo('/pages/order/order?state=4')" hover-class="common-hover" :hover-stay-time="50">
					<text class="yticon icon-shouhoutuikuan"></text>
					<text>退款/售后</text>
				</view>
			</view>
			
			<view class="user-vip" @click.stop="navTo('/pages/user/vip/list')">
				<image src="/static/icon/vip_card_icon.png" class="bg-vip" mode=""></image>
				<view class="add-vip">
					<view class="left">
						<image src="/static/icon/icon_vip_02.png" class="vip-icon" mode=""></image>
						<view class="vip-text">加入VIP尊享特权</view>
					</view>
					<image src="/static/icon/right_arrow_02.png" class="vip-r-icon" mode=""></image>
				</view>
			</view>
			
			<!-- 积分 -->
			<view class="user-point">
				<view class="point-title">积分</view>
				<view class="point-row">
					<view class="point-item point-bd">
						<view class="num">{{userDetail.xiaofei_credit_day || 0}}</view>
						<view class="item-name">今日消费积分</view>
					</view>
					<view class="point-item point-bd" @click="linkto(`/subPackage/balance/balance?type=3`)">
						<view class="num">{{userDetail.freedMoney || 0}}</view>
						<view class="item-name">冻结积分</view>
					</view>
					<view class="point-item ">
						<view class="num">{{userDetail.freedcredit_day || 0}}</view>
						<view class="item-name">今日释放积分</view>
					</view>
					<view class="point-item point-bd" @click="linkto('/subPackage/balance/balance')">
						<view class="num">{{userDetail.userMoney || 0}}</view>
						<view class="item-name">红包</view>
					</view>
					<view class="point-item point-bd" @click="linkto(`/pages/point`)">
						<view class="num">{{userDetail.duije_credit || 0}}</view>
						<view class="item-name">对接积分</view>
					</view>
					<view class="point-item ">
						<view class="num">{{userDetail.team_sale || 0}}</view>
						<view class="item-name">团队业绩</view>
					</view>
				</view>
			</view>
			

			<!-- 浏览历史 v-if="!userDetail.is_shop v-if="userDetail.is_shop""-->
			<view class="history-section icon">
				<!-- <list-cell  title="对接积分" @eventClick="navTo('/subPackage/myorderdetail/myorderdetail')">
					<image src="/static/images/icon_duijiejifen.png" mode=""></image>
				</list-cell> -->
				
				<view class="cell-item" @click.stop="navTo('/pages/user/apply')">
					<view class="item-box">
						<view class="item-img">
							<image class="cell-icon" src="/static/icon/icon_shenqingdanli.png" mode=""></image>
						</view>
						<view class="cell-l">
							<view>申请代理</view>
							<text class="cell-more yticon icon-you"></text>
						</view>				
					</view>
				</view>

        <view class="cell-item" @click.stop="navTo('/pages/user/followShop')">
          <view class="item-box">
            <view class="item-img">
              <image class="cell-icon" src="/static/icon/dpgz.png" mode=""></image>
            </view>
            <view class="cell-l">
              <view>关注的店铺</view>
              <text class="cell-more yticon icon-you"></text>
            </view>
          </view>
        </view>


				<view class="cell-item" @click.stop="navTo('/pages/point')">
					<view class="item-box">
						<view class="item-img">
							<image class="cell-icon" src="/static/images/icon_duijiejifen.png" mode=""></image>
						</view>
						<view class="cell-l">
							<view>对接积分</view>
							<text class="cell-more yticon icon-you"></text>
						</view>				
					</view>
				</view>
				<view class="cell-item" @click.stop="realFn()">
					<view class="item-box">
						<view class="item-img">
							<image class="cell-icon" src="/static/icon/icon_renzheng.png" mode=""></image>
						</view>
						<view class="cell-l">
							<view>实名认证</view>
							<text class="cell-more yticon icon-you"></text>
						</view>				
					</view>
				</view>
				<list-cell icon="icon-order1" :iconfont="true" iconColor="#e07472" title="订单信息" @eventClick="navTo('/subPackage/myorderdetail/myorderdetail')"></list-cell>
				<list-cell icon="icon-dingdanxinxi" :iconfont="true"  iconColor="#e07472" @eventClick="navTo('/district/storeOrder/storeOrder')"
				 title="店铺订单"></list-cell>
				<list-cell icon="icon-pintuan" :iconfont="true"  iconColor="#e07472" title="我的拼团" @eventClick="navTo('/subPackage/grouplist/grouplist')"></list-cell>
				<list-cell icon="icon-shoukuanma" :iconfont="true"  iconColor="#e07472" title="我的收款码" @eventClick="navTo('/subPackage/uploadimg/uploadimg')"></list-cell>
				<!-- <list-cell icon="icon-iconfontweixin" iconColor="#e07472" title="商城收益"></list-cell> -->
				<list-cell icon="icon-dizhi" iconColor="#5fcda2" title="地址管理" @eventClick="navTo('/pages/address/address')"></list-cell>
				<list-cell icon="icon-shangjiaguanli" :iconfont="true" iconColor="#5fcda2" title="商家管理" @eventClick="navTo('/subPackage/shoptree/shoptree')"></list-cell>
				<list-cell icon="icon-gongxianzhi" :iconfont="true" iconColor="#5fcda2" title="贡献值管理" @eventClick="navTo('/subPackage/vipmanagement/vipmanagement')"></list-cell>
				<list-cell icon="icon-dingdan" :iconfont="true" iconColor="#e07472" v-if="userDetail.canJoinTrader" @eventClick="checkin"
				 title="商家入驻申请"></list-cell>
			</view>
			<!-- v-if="userDetail.canJoinTrader"
v-if="userDetail.canJoinTrader" -->
			<view class="history-section icon">
				<list-cell icon="icon-bangzhu" iconColor="#5fcda2" title="更改手机号码" @eventClick="modifyPhone"></list-cell>
				<list-cell icon="icon-bangzhu" iconColor="#5fcda2" title="绑定手机号" @eventClick="navTo('/subPackage/bindmobile/bindmobile')"></list-cell>
				<list-cell icon="icon-share" iconColor="#9789f7" title="机构申请" v-if="userDetail.canJoinTrader" @eventClick="navTo('/subPackage/mechanism/mechanism')"></list-cell>
				<!-- <list-cell icon="icon-iconfontweixin" iconColor="#e07472" title="消费积分"></list-cell> -->
				<!-- <list-cell icon="icon-iconfontweixin" iconColor="#e07472" title="现金积分"></list-cell> -->
				<!-- <list-cell icon="icon-iconfontweixin" iconColor="#e07472" title="兑换积分"></list-cell> -->

				<list-cell icon="icon-pinglun-copy" iconColor="#ee883b" title="余额记录" @eventClick="navTo('/subPackage/balance/balance')"></list-cell>
				<list-cell icon="icon-pinglun-copy" iconColor="#ee883b" title="消费记录" @eventClick="navTo('/subPackage/trunoverList/trunoverList')"></list-cell>
				<!-- <list-cell icon="icon-iconfontweixin" iconColor="#e07472" title="销售总额"></list-cell> -->
				<list-cell icon="icon-iconfontweixin" iconColor="#e07472" @eventClick="navTo('/subPackage/income/income')" title="让利值记录"></list-cell>
				<!-- <list-cell icon="icon-iconfontweixin" iconColor="#e07472" title="消费积分转账"></list-cell> -->
				<list-cell icon="icon-xinyongkahuankuan" :iconfont="true" iconColor="#e07472" @eventClick="navTo('/subPackage/bankCard/bankCard')"
				 title="我的银行卡"></list-cell>

        <list-cell icon="icon-pingtaijianjie" :iconfont="true" iconColor="#e07472" @eventClick="navTo('/pages/service/aboutus')"
                   title="平台简介"></list-cell>

        <list-cell icon="icon-kefu1" iconColor="#e07472" :iconfont="true" @eventClick="navTo('/pages/service/customer')"
                   title="专属客服"></list-cell>

        <list-cell icon="icon-tousu1" iconColor="#e07472" :iconfont="true" @eventClick="navTo('/pages/service/complaint')"
                   title="投诉/建议"></list-cell>
			</view>

			<view class="history-section icon">
				<list-cell icon="icon-tixianshenqing" iconColor="#54b4ef" title="提现申请" @eventClick="navTo('/subPackage/withdrawal/withdrawal')"></list-cell>
				<list-cell icon="icon-tixianjilu-copy" iconColor="#54b4ef" title="提现记录" @eventClick="navTo('/subPackage/withdrawalList/withdrawalList')"></list-cell>
				<!-- <list-cell icon="icon-pinglun-copy" iconColor="#ee883b" title="晒单" tips="晒单抢红包"></list-cell> -->
				<!-- <list-cell icon="icon-shoucang_xuanzhongzhuangtai" iconColor="#54b4ef" title="我的收藏"></list-cell> -->
				<!-- <list-cell icon="icon-shezhi1" iconColor="#e07472" title="设置" @eventClick="navTo('/pages/set/set')"></list-cell> -->
				<!-- #ifdef APP-PLUS -->
				<!-- 		<list-cell icon="icon-shoucang_xuanzhongzhuangtai" iconColor="#54b4ef" title="呱呱积分" :tips="userDetail?userDetail.gg_credit:''"
				 @eventClick="navTo('/subPackage/scoreMarket/scoreMarket')"></list-cell> -->
				<!-- #endif -->
				<!-- <list-cell icon="icon-bangzhu" iconColor="#5fcda2" title="修改登录密码" @eventClick="navTo('/subPackage/forgetPwd/forgetPwd?type=reset')"></list-cell> -->

				<list-cell icon="icon-xiugaimima" :iconfont="true" iconColor="#5fcda2" title="设置登录密码" @eventClick="navTo('/subPackage/forgetPwd/forgetPwd?type=forget')"></list-cell>
				<list-cell icon="icon-xiugaimima" :iconfont="true" iconColor="#5fcda2" title="修改支付密码" @eventClick="navTo('/subPackage/payPwd/payPwd?type=reset')"></list-cell>
				<!-- <list-cell icon="icon-hot" iconColor="#54b4ef" title="我的团队" @eventClick="navTo('/subPackage/chartsTree/chartsTree?userImg='+userDetail.userPhoto)"></list-cell> -->
				<list-cell icon="icon-tuichu" border="" :iconfont="true" iconColor="#e07472" @eventClick="loginOut" title="退出登陆"></list-cell>
			</view>
		</view>
		<resetPhone ref='resetPhone'></resetPhone>

	</view>
</template>
<script>
	import listCell from '@/components/mix-list-cell';

	import resetPhone from "@/components/resetPhone/resetPhone.vue"
	import sitInfo from "@/siteInfo.js"
	import {
		userInfo
	} from "@/common/loadUserInfo.js"
	import {
		mapState
	} from 'vuex';
	let startY = 0,
		moveY = 0,
		pageAtTop = true;
	export default {
		components: {
			listCell,
			resetPhone
		},
		data() {
			return {
				coverTransform: 'translateY(0px)',
				coverTransition: '0s',
				moving: false,
				userDetail: null,
				vipAble: require('../../static/images/vipenable.png'),
				vipEnable: require('../../static/images/vipdisable.png'),
				hasToken: false,
				pic: '',
				username: '',
				user: {},
        userInfo:{},
        parentUser:{},

      }
		},
		onPullDownRefresh: function() {
			let that = this;
			this.hasToken = uni.getStorageSync('userInfo') ? true : false
			if (that.hasToken) {
				userInfo({
					success: (res) => {
						that.userDetail = res.data.userinfo
						uni.setStorageSync('userDetail', that.userDetail)
						uni.stopPullDownRefresh()
					},
					fail: (err) => {


						uni.navigateTo({
							url: '/pages/public/login'
						})

					}
				})
			} else {

				uni.navigateTo({
					url: '/pages/public/login'
				})
			}
		},
		onLoad() {
			let that = this;
			// #ifdef MP-WEIXIN
			if (that.hasToken) {
				wx.login({
					provider: 'weixin',
					success: function(loginRes) {
						console.log(loginRes)
						let code = loginRes.code
						that.$Request.request('/api/users/openid', 'POST', {
							code
						}).then(res => {
				
						})
						// 设置用户openid
					}
				});
			}
			// #endif

      if(uni.getStorageSync('userDetail')){
        this.userInfo = uni.getStorageSync('userDetail')
        // console.log(JSON.stringify(this.userInfo))
      }
      if (uni.getStorageSync('userDetail').parentUser) {
        this.parentUser = uni.getStorageSync('userDetail').parentUser
      }
      console.log(this.parentUser);
    },
		onShow() {
			let that = this;
			this.hasToken = uni.getStorageSync('userInfo') ? true : false
			if (that.hasToken) {
				console.log(2)
				userInfo({
					success: (res) => {
						that.userDetail = res.data.userinfo
						uni.setStorageSync('userDetail', that.userDetail)
					},
					fail: (err) => {
						uni.navigateTo({
							url: '/pages/public/login'
						})
						console.log(err)

					}
				})
			} else {
				uni.navigateTo({
					url: '/pages/public/login'
				})
			}
			if (uni.getStorageSync('user')) {
				this.user = uni.getStorageSync('user')
			}

		},
		// #ifndef MP
		onNavigationBarButtonTap(e) {
			const index = e.index;
			if (index === 0) {
				this.navTo('/pages/set/set');
			} else if (index === 1) {
				// #ifdef APP-PLUS
				const pages = getCurrentPages();
				const page = pages[pages.length - 1];
				const currentWebview = page.$getAppWebview();
				currentWebview.hideTitleNViewButtonRedDot({
					index
				});
				// #endif
				uni.navigateTo({
					url: '/pages/notice/notice'
				})
			}
		},
		// #endif

		methods: {
			// 是否可以进入实名认证
			realFn(){
				if(this.userDetail.is_verified == 1){
					return this.$api.msg('您进行过实名认证，无需再次认证')
				}
				this.navTo('/pages/user/real')
			},
			checkin() {
				uni.navigateTo({
					url: '../../subPackage/applicationlist/applicationlist'
				})
			},
			topricedetail() {

			},
			toassetbag() {
				// #ifdef APP-PLUS

				uni.navigateTo({
					url: '../../subPackage/assetbag/assetbag'
				})

				// #endif

				//#ifdef H5 || MP-WEIXIN
				uni.navigateTo({
					url: '../../subPackage/assetbag/assetbag'
				})
				// this.$api.msg('暂未开放')

				//#endif
			},
			toShare() {
				let that = this
				if (!that.hasToken) {
					uni.navigateTo({
						url: '/pages/public/login'
					})
					return
				}
				let url = sitInfo.apiroot + "/mregister.html?mid=" + that.userDetail.userId
				uni.setClipboardData({
					data: url,
					success: () => {
						that.$api.msg('分享链接已复制到剪切板！')
					}
				})

			},
			loginOut() {
				uni.clearStorageSync()
				this.user = {}
				setTimeout(() => {
					uni.navigateTo({
						url: '/pages/public/login'
					})
				}, 800)
			},
			modifyPhone() {
				uni.navigateTo({
					url: '../../subPackage/changemobile/changemobile'
				})
			},
			editUserinfo() {
				if (this.hasToken) {
					uni.navigateTo({
						url: '/pages/userinfo/userinfo?userDetail=' + JSON.stringify(this.userDetail)
					})
				} else {
					uni.navigateTo({
						url: '../public/login'
					})
				}

			},
			clickTest() {
				// return this.$api.msg('功能暂未开放');
				uni.navigateTo({
					url: '/pages/user/recharge'
				})
			},
			/**
			 * 统一跳转接口,拦截未登录路由
			 * navigator标签现在默认没有转场动画，所以用view
			 */
			navTo(url) {
				console.log(this.hasToken)
				if (!this.hasToken) {
					url = '/pages/public/login'
				}
				uni.navigateTo({
					url
				})
			},
			linkto(url) {
				console.log(this.hasToken)
				if (!this.hasToken) {
					url = '/pages/public/login'
				}
				uni.navigateTo({
					url
				})
			},
			/**
			 *  会员卡下拉和回弹
			 *  1.关闭bounce避免ios端下拉冲突
			 *  2.由于touchmove事件的缺陷（以前做小程序就遇到，比如20跳到40，h5反而好很多），下拉的时候会有掉帧的感觉
			 *    transition设置0.1秒延迟，让css来过渡这段空窗期
			 *  3.回弹效果可修改曲线值来调整效果，推荐一个好用的bezier生成工具 http://cubic-bezier.com/
			 */
			coverTouchstart(e) {
				if (pageAtTop === false) {
					return;
				}
				this.coverTransition = 'transform .1s linear';
				startY = e.touches[0].clientY;
			},
			coverTouchmove(e) {
				moveY = e.touches[0].clientY;
				let moveDistance = moveY - startY;
				if (moveDistance < 0) {
					this.moving = false;
					return;
				}
				this.moving = true;
				if (moveDistance >= 80 && moveDistance < 100) {
					moveDistance = 80;
				}

				if (moveDistance > 0 && moveDistance <= 80) {
					this.coverTransform = `translateY(${moveDistance}px)`;
				}
			},
			coverTouchend() {
				if (this.moving === false) {
					return;
				}
				this.moving = false;
				this.coverTransition = 'transform 0.3s cubic-bezier(.21,1.93,.53,.64)';
				this.coverTransform = 'translateY(0px)';
			}
		}
	}
</script>
<style lang='scss'>
	.user-section .user-info-box .vipInfo {
		position: absolute;
		top: 50%;
		transform: translateY(-50%);
		right: 0;
	}

  .user-order{
    display: flex;
    justify-content: space-between;
    font-size:28rpx;
    background:#ffffff;
    border-radius: 10rpx;
    border-bottom:1rpx solid #f2f1f1;
    height: 100rpx;
    line-height: 100rpx;
  }
  .user-order .button {
    width: 300rpx;
    font-size: 28rpx;

    color: #DD1A21;
    background: #fff;
    border-radius: 10rpx;
    margin: 10rpx auto;
    background: #f2f2f2;
    display: block;
  }


  .user-order .order .view-order{text-align:right;font-size:20rpx;color:#999}

  .referrer{
    background: #b47608;
    color: white;
    padding: 4rpx;
    border-radius: 6rpx;
    font-size: 24rpx;
  }
  @import './user.scss'
</style>
